//
//  GoodsPropertySelectedView.m
//  quxiangtao
//
//  Created by xbmios on 2018/8/22.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "GoodsPropertySelectedView.h"
#import "DCCollectionHeaderLayout.h"


static NSString *goodsCellID = @"goodsCellID";
static NSString *propertyGoodsCellID = @"propertyGoodsCellID";

@interface GoodsPropertySelectedView ()<UITableViewDelegate, UITableViewDataSource, HorizontalCollectionLayoutDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) UIView *coverView;

@property (strong, nonatomic) UIView *selectedView;

@property (strong, nonatomic) UITableView *goodsTableView;
@property (strong, nonatomic) UICollectionView *propertyCollectionView;

@property (strong, nonatomic) UIView *bottomView;
//@property (strong, nonatomic) UIView *addToCartView;
//@property (strong, nonatomic) UILabel *addToCartL;

@property (strong, nonatomic) UIView *purchaseView;
@property (strong, nonatomic) UILabel *purchaseL;

@property (assign, nonatomic) CGFloat height;
@property (assign, nonatomic) NSInteger controllerType;

@end

@implementation GoodsPropertySelectedView

- (instancetype)initWithFrame:(CGRect)frame withHeight:(CGFloat)height andControllerType:(NSInteger)type {
    
    if (self = [super initWithFrame:frame]) {
        
        self.height = height;
        self.controllerType = type;
        
        self.backgroundColor = [UIColor clearColor];
        
        [self addSubview:self.coverView];
        [self addSubview:self.selectedView];
        
        [self.selectedView addSubview:self.goodsTableView];
        [self.selectedView addSubview:self.propertyCollectionView];
        
        [self.selectedView addSubview:self.bottomView];
        
//        [self.bottomView addSubview:self.addToCartView];
//        [self.addToCartView addSubview:self.addToCartL];
        
        [self.bottomView addSubview:self.purchaseView];
        [self.purchaseView addSubview:self.purchaseL];
        
        [self createUI];
        
    }

    return self;
}

#pragma mark -tableview Delegate / datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:goodsCellID];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

#pragma mark -collection Delegate \ datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 5;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:propertyGoodsCellID forIndexPath:indexPath];
    
    return cell;
    
}

- (void)createUI {
    
    self.coverView.frame = self.bounds;
    self.selectedView.frame = CGRectMake(0, HEIGHT, WIDTH, self.height);
    
    self.goodsTableView.frame = CGRectMake(0, 0, WIDTH, jisuan(200));
    self.goodsTableView.rowHeight = jisuan(200);
    self.goodsTableView.backgroundColor = Color1f89ff;
    
    self.bottomView.frame = CGRectMake(0, self.height - jisuan(100), WIDTH, jisuan(100));
    self.bottomView.backgroundColor = Colorff0000;
    
//    self.addToCartView.frame = CGRectMake(0, 0, WIDTH / 2.0, jisuan(100));
//    self.addToCartL.frame = CGRectMake(0, 0, WIDTH / 2.0, jisuan(100));
    
    self.purchaseView.frame = CGRectMake(0, 0, WIDTH, jisuan(100));
    self.purchaseL.frame = CGRectMake(0, 0, WIDTH, jisuan(100));
    
    self.propertyCollectionView.frame = CGRectMake(0, jisuan(200), WIDTH, self.height - jisuan(200) - jisuan(100));
    self.propertyCollectionView.backgroundColor = Colordddddd;

}

#pragma mark -黑色背景手势
- (void)coverGestureClick:(UIGestureRecognizer *)recognizer {
    
    [self hidden];
}

#pragma mark -显示
- (void)show {
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.selectedView.centerY = self.selectedView.centerY - CGRectGetHeight(self.selectedView.frame);
   
    }];
    
}

#pragma mark -隐藏
- (void)hidden {
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.selectedView.centerY = self.selectedView.centerY + CGRectGetHeight(self.selectedView.frame);
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];
    
}

#pragma mark -立即购买点击
- (void)purchaseClick:(UIGestureRecognizer *)recognizer {
    
    // 需要判断是否选择了颜色、尺寸等属性
    // 有选择 界面消失 或者 跳转到别的视图
    // 无选择 提示未选择属性

//    self.controllerType ? NSLog(@"立即进货") : NSLog(@"立即购买");
    
    [UIView animateWithDuration:0.1 animations:^{

        [self hidden];
        
    } completion:^(BOOL finished) {
        
        [self.presentingVC.navigationController popViewControllerAnimated:YES];
        
    }];

}

//#pragma mark -加购点击
//- (void)addCartClick:(UIGestureRecognizer *)recognizer {
//    
//    NSLog(@"加购点击");
//    
//    [self hidden];
//}

#pragma mark -lazy
- (UIView *)coverView {
    
    if (!_coverView) {
    
        _coverView = [[UIView alloc] init];
        _coverView.backgroundColor = Color333333;
        _coverView.alpha = 0.6;
    
        UITapGestureRecognizer *coverGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverGestureClick:)];
        [_coverView addGestureRecognizer:coverGesture];
        
    }
    
    return _coverView;
}

- (UIView *)selectedView {
    
    if (!_selectedView) {
        
        _selectedView = [[UIView alloc] init];
        _selectedView.backgroundColor = WhiteColor;
        
    }
    
    return _selectedView;
}

- (UITableView *)goodsTableView {
    
    if (!_goodsTableView) {
        
        _goodsTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        
        _goodsTableView.delegate = self;
        _goodsTableView.dataSource = self;
        
        _goodsTableView.scrollEnabled = NO;
        
        _goodsTableView.showsVerticalScrollIndicator = NO;
        _goodsTableView.showsHorizontalScrollIndicator = NO;
        
        _goodsTableView.estimatedRowHeight = 0;
        _goodsTableView.estimatedSectionHeaderHeight = 0;
        _goodsTableView.estimatedSectionFooterHeight = 0;
        
        _goodsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [_goodsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:goodsCellID];
        
        if (@available(iOS 11.0, *)) {
            
            _goodsTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        }
        
    }
    
    return _goodsTableView;
    
}

- (UICollectionView *)propertyCollectionView {
    
    if (!_propertyCollectionView) {

//        DCCollectionHeaderLayout *flowLayout = [[DCCollectionHeaderLayout alloc] init];
//        flowLayout.delegate = self;
//        flowLayout.lineSpacing = 8.0;
//        flowLayout.interitemSpacing = 10;
//        flowLayout.headerViewHeight = 35;
//        flowLayout.footerViewHeight = 5;
//        flowLayout.itemInset = UIEdgeInsetsMake(0, 10, 0, 10);
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

        _propertyCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        
        _propertyCollectionView.delegate = self;
        _propertyCollectionView.dataSource = self;
        _propertyCollectionView.alwaysBounceVertical = YES;
        
        _propertyCollectionView.showsVerticalScrollIndicator = NO;
        _propertyCollectionView.showsHorizontalScrollIndicator = NO;
        
        [_propertyCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:propertyGoodsCellID];

        if (@available (iOS 11.0, *)) {
            
            _propertyCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
    }
    
    return _propertyCollectionView;
}

- (UIView *)bottomView {
    
    if (!_bottomView) {
    
        _bottomView = [[UIView alloc] init];

    }
    
    return _bottomView;
}
//- (UIView *)addToCartView {
//
//    if (!_addToCartView) {
//
//        _addToCartView = [[UIView alloc] init];
//        _addToCartView.backgroundColor = Colorff0000;
//
//        UITapGestureRecognizer *addCartGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addCartClick:)];
//        [_addToCartView addGestureRecognizer:addCartGesture];
//
//    }
//
//    return _addToCartView;
//}
//- (UILabel *)addToCartL {
//
//    if (!_addToCartL) {
//
//        _addToCartL = [[UILabel alloc] init];
//        _addToCartL.text = @"加入购物车";
//        _addToCartL.textAlignment = NSTextAlignmentCenter;
//        _addToCartL.textColor = WhiteColor;
//        _addToCartL.font = Font(30);
//
//    }
//    return _addToCartL;
//}

- (UIView *)purchaseView {
    
    if (!_purchaseView) {
        
        _purchaseView = [[UIView alloc] init];
        
        _purchaseView.backgroundColor = self.controllerType ? Color1f89ff : Colorff0000;
        
        UITapGestureRecognizer *purchaseGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(purchaseClick:)];
        [_purchaseView addGestureRecognizer:purchaseGesture];
        
    }
    
    return _purchaseView;
}
- (UILabel *)purchaseL {
    
    if (!_purchaseL) {
        
        _purchaseL = [[UILabel alloc] init];
        _purchaseL.text = @"立即购买";
        _purchaseL.textAlignment = NSTextAlignmentCenter;
        _purchaseL.textColor = WhiteColor;
        _purchaseL.font = Font(30);
        
    }
    return _purchaseL;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
